
#ifndef _NEBULA_SDK__TASK_PROCESSOR_H__INCLUDED_
#define _NEBULA_SDK__TASK_PROCESSOR_H__INCLUDED_

#include <memory>

#include "Queue.h"
#include "Task.h"

namespace nebula { namespace sdk {

template <typename service_type>
class TaskProcessorImpl;

template <typename service_type>
class TaskProcessor
{
public:
	typedef NEBULA_ERROR_CODE (service_type::*task_processor)(std::auto_ptr<ITask> task);

public:
	TaskProcessor(const char* name) :
		pImpl(new TaskProcessorImpl<service_type>(name))
	{
	}

public:
	NEBULA_ERROR_CODE start(
			service_type *service,
			task_processor proc,
			ITodoTaskQueue *queue,
			unsigned min_thread_count = 1,
			unsigned max_thread_count = 1
			)
	{
		return pImpl->start(service, proc, queue, min_thread_count, max_thread_count);
	}

	NEBULA_ERROR_CODE stop()
	{
		return pImpl->stop();
	}

	// asynchronously notify the thread pool to stop, you still have
	// to call 'stop' to recycle the resource
	NEBULA_ERROR_CODE notify_to_stop()
	{
		return pImpl->notify_to_stop();
	}

	//should not be called before start()
	NEBULA_ERROR_CODE set_thread_count_range(
			unsigned int min_thread_count,
			unsigned int max_thread_count
			)
	{
		return pImpl->set_thread_count_range(min_thread_count, max_thread_count);
	}

private:
	std::auto_ptr<TaskProcessorImpl<service_type> > pImpl;
};

}}

#include "TaskProcessorImpl.h"
#endif // _NEBULA_SDK__TASK_PROCESSOR_H__INCLUDED_

